<template>
	<view>
		<view class="list" v-for="(item, index) in collect" :key="index" :for-id="index">
			<view class='collect-title'>
				<view class="collect-img-box">
					<image class="collect-img" :src="item.store_image" mode="aspectFit"></image>
				</view>
				<view>{{item.store_name}}</view>
			</view>
			<view class="m-list">
				<view class="list-left">
					<image :src="item.goods_img" mode="widthFix"></image>
					<view v-if="!item.goods_name">该商品下架/淘汰啦！</view>
				</view>
				<view class="list-right">
					<view class="name">{{item.goods_name?item.goods_name:'商品名称'}}</view>
					<block v-if="item.status==1&&item.act_price<item.online_price">
						<view class="price">￥{{item.act_price}}<text class="price_pass"><text
									class="pass">￥{{item.online_price}}</text></text>
						</view>
					</block>
					<block v-else>
						<view v-if="item.plus_mem_status == 1 && (user_type == 2 || plus_register_state == 1)">
							<view class="price">
								<text class="plus-price-color">￥{{item.plus_price}}</text>
								<text class="plus-price-icon">PLUS</text>
							</view>
						</view>
						<view v-if="item.shop_mem_status==1&& (user_type==3 || vip_register_state == 1)">
							<view class="price">
								<text class="price-color">￥{{item.shop_mem_price}}</text>
								<text class="vip-price-icon">VIP</text>
							</view>
						</view>
						<view v-else>
							<view class="price">￥{{item.online_price?item.online_price:'商品价格'}}</view>
						</view>
					</block>
					<view class="btn">
						<view class="del" @tap="del" :id="index" :data-goodid="item.goods_id"
							:data-store_id="item.store_id">删除</view>
						<view class="buy" @tap="Settlement" :data-goodid="item.goods_id" :data-store_id="item.store_id">
							立即购买</view>
					</view>
				</view>
			</view>
		</view>
		<view class="loading">
			<block v-if="isLoading">
				<image src="https://mall.xzthealth.com/loading2.gif"></image>
				<text>加载中</text>
			</block>
			<text v-else-if="canLoad">下拉加载更多</text>
			<text v-else-if="!canLoad&&(!collect||!collect.length)">暂无商品</text>
			<text v-else>已加载全部</text>
		</view>
	</view>
</template>

<script>
	var app = getApp().globalData;

	export default {
		data() {
			return {
				user_type: 1,
				collect: [],
				isLoading: true,
				canLoad: true,
				page: 0
			};
		},

		components: {},
		props: {},
		onLoad(options) {
			// #ifdef MP-ALIPAY
			if (JSON.stringify(options) == '{}') {
				options = my.getLaunchOptionsSync().query;
			}
			// #endif
			if (options && (options.store_id)) {
				this.$store.store_id = options.store_id;
			}
		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			this.getGoods();
		},


		async onShow() {
			await this.$onLaunched;
			this.setData({
				page: 0,
				canLoad: true,
				isLoading: true
			});
			this.getGoods();
		},

		onShareAppMessage(res) {
			return {
				title: '我的收藏',
				path: '/pages/mine/MyCollect/MyCollect?store_id=' + this.$store.store_id +
					'&share_uid=' + app.XcxData.userInfo.uid,
				success: function(res) { // 转发成功
				},
				fail: function(res) { // 转发失败
				}
			};
		},
		computed: {
			plus_register_state: () => {
				return app.XcxData.plus_register_state
			},
			vip_register_state: () => {
				return app.XcxData.vip_register_state
			}

		},
		methods: {
			del: function(e) {
				var collect = this.collect;
				var id = e.currentTarget.id;
				var goodid = e.currentTarget.dataset.goodid;
				var store_id = e.currentTarget.dataset.store_id;
				var that = this;
				console.log(e);
				uni.showModal({
					title: '提示',
					content: '是否删除该收藏',
					success: function(res) {
						if (res.confirm) {
							collect.splice(id, 1);
							getApp().globalData.sendRequest({
								url: '/Collect/cancelCollect',
								type: "post",
								data: {
									goods_id: goodid,
									store_id: store_id
								},
								success: function(res) {
									if (res.status == 1) {
										that.setData({
											collect: collect
										});
									} else {
										uni.showToast({
											title: res.msg,
											icon: 'none',
											duration: 1500,
											mask: true
										});
									}
								}
							});
						}
					}
				});
			},
			Settlement: function(e) {
				var goodid = e.currentTarget.dataset.goodid;
				uni.navigateTo({
					url: '/pages/mer/goods/details/details?id=' + goodid
				});
			},

			getGoods() {
				var that = this;
				var page = parseInt(this.page) + 1;

				if (this.canLoad) {
					this.setData({
						canLoad: false,
						isLoading: true
					});
					getApp().globalData.sendRequest({
						url: '/Collect/myCollectList_v2',
						type: "post",
						data: {
							page: page
						},
						success: function(res) {
							if (res.status == 1) {
								var collect = that.collect;

								if (page != 1) {
									//第一页
									collect.push.apply(collect, res.my_collect);
								} else {
									collect = res.my_collect;
								}

								var canLoad = true;

								if (res.my_collect.length < res.limit) {
									canLoad = false;
								}

								that.setData({
									collect: collect,
									user_type: res.user_type ? res.user_type : 1,
									page: page,
									canLoad: canLoad,
									isLoading: false
								});
							} else {
								uni.showModal({
									title: '错误提示',
									content: res.msg ? res.msg : '请求出错了',
									showCancel: false
								});
							}
						}
					});
				}
			}

		}
	};
</script>
<style>
	@import "./MyCollect.css";

	.puls_price_icon {
		width: 70rpx;
		height: 30rpx;
	}
</style>
